package ms.shop.controller;

import com.alibaba.fastjson.JSONObject;
import ms.common.exception.BizException;
import ms.common.page.Page;
import ms.common.util.string.StringUtil;
import ms.shop.bean.GoodParamBean;
import ms.shop.bean.GoodQueryParamBean;
import ms.shop.config.ResponseCode;
import ms.shop.entity.Good;
import ms.shop.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Project Name: ms
 * File Name: GoodController
 * Package Name: ms.shop.controller
 * Date: 2018/5/31  16:24
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@RestController
@RequestMapping("/good")
public class GoodController {
    
    @Autowired
    private GoodService goodService;
    /**
     * 分页查询
     * @param goodQueryParamBean
     * @return
     */
    @PostMapping("/query")
    public Page<Good> query(@RequestBody GoodQueryParamBean goodQueryParamBean) {
        if(StringUtil.isEmpty(goodQueryParamBean.getPageNo())) {
            goodQueryParamBean.setPageNo(1);
        }
        if(StringUtil.isEmpty(goodQueryParamBean.getPageSize())) {
            goodQueryParamBean.setPageSize(10);
        }
        if(StringUtil.isEmpty(goodQueryParamBean.getName())) {
            goodQueryParamBean.setName("");
        }
        if(StringUtil.isEmpty(goodQueryParamBean.getStartPrice())) {
            goodQueryParamBean.setStartPrice(0L);
        }
        if(StringUtil.isEmpty(goodQueryParamBean.getEndPrice())) {
            goodQueryParamBean.setEndPrice(0L);
        }
        if(StringUtil.isEmpty(goodQueryParamBean.getStatus())) {
            goodQueryParamBean.setStatus(0);
        }
        if(StringUtil.isEmpty(goodQueryParamBean.getClassifyId())) {
            goodQueryParamBean.setClassifyId(0L);
        }


        String key = JSONObject.toJSONString(goodQueryParamBean.getName());
        if("\"\"".equals(key)) {
            key = "";
        }
        Page<Good> page = goodService.query(goodQueryParamBean, key);
        return page;
    }

    /**
     * 新增
     * @param goodParamBean
     * @return
     */
    @PostMapping("/create")
    public void create(@RequestBody GoodParamBean goodParamBean) {
        //参数校验
        checkParam(goodParamBean);

        goodService.create(goodParamBean);
    }

    /**
     * 修改
     * @param goodParamBean
     * @return
     */
    @PostMapping("/update")
    public void update(@RequestBody GoodParamBean goodParamBean) {
        //参数校验
        checkParam(goodParamBean);

        goodService.update(goodParamBean);
    }


    /**
     * 启用
     * @param id
     * @return
     */
    @PostMapping("/use/{id}")
    public void use(@PathVariable("id") Long id) {
        goodService.use(id);
    }

    /**
     * 停用
     * @param id
     * @return
     */
    @PostMapping("/cancel/{id}")
    public void cancel(@PathVariable("id") Long id) {
        goodService.cancel(id);
    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping("/detail/{id}")
    public Good detail(@PathVariable("id") Long id) {
        return goodService.detail(id);
    }

    /**
     * 清除缓存
     * @return
     */
    @PostMapping("/clear")
    public void clear() {
        goodService.clear();
    }

    /**
     * 校验新增和修改参数
     * @param goodParamBean
     */
    private void checkParam(GoodParamBean goodParamBean) {
        if(StringUtil.isEmpty(goodParamBean.getId())) {
            if(StringUtil.isEmpty(goodParamBean.getImg())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "图片不能为空");
            }
            if(StringUtil.isEmpty(goodParamBean.getClassifyId())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "分类不能为空");
            }
            if(StringUtil.isEmpty(goodParamBean.getIntroduce())) {
                throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "商品介绍不能为空");
            }
            if(StringUtil.isEmpty(goodParamBean.getStatus())) {
                goodParamBean.setStatus(2);
            }
        }
        if(StringUtil.isEmpty(goodParamBean.getName())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "商品名称不能为空");
        }
        if(StringUtil.isEmpty(goodParamBean.getMemo())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "商品描述不能为空");
        }
        if(StringUtil.isEmpty(goodParamBean.getPrice())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "商品价格不能为空");
        }
        if(StringUtil.isEmpty(goodParamBean.getRealPrice())) {
            goodParamBean.setRealPrice(goodParamBean.getPrice());
        }
    }
}
